
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

local function PostDelayCb(scene, cb, interval)
   local scheduler = scene:getScheduler()
   local id
    id = scheduler:scheduleScriptFunc(function()
        scheduler:unscheduleScriptEntry(id)
        cb()
    end, interval, false)
end

-------------------------------------------
-- 节点事件
-------------------------------------------
local function TestNodeEvent(self)
    local function CreateTestScene(name)
        local scene = display.newScene(name)
        scene:registerScriptHandler(function(event) 
            printf("node in scene [%s] NODE_EVENT:%s", name, event)
            end)
        return scene
    end

    PostDelayCb(self, function()
        local scene1 = CreateTestScene("scene1")
        display.runScene(scene1)

        PostDelayCb(scene1, function()
            release_print("--------------------")
            local scene2 = CreateTestScene("scene2")
            -- 通过注释选择不同的效果再次观察输出
            -- display.runScene(scene2)
            display.runScene(scene2, "random", 1.0)
        end, 1.0)
    end, 1.0)
end

-------------------------------------------
-- 帧事件
-------------------------------------------
local function TestFrameEvent(self)
    local scene = display.newScene()
    self:addChild(scene)

    local function PostFrameEvent()
        scene:scheduleUpdateWithPriorityLua(function(delta)
            print("frame event, delta:" .. delta)
        end, 0)
    end

    PostFrameEvent()

    PostDelayCb(scene, function()
        scene:unscheduleUpdate()
        print("frame event stop, but will start again")

        PostDelayCb(scene, function()
            PostFrameEvent()
        end, 1.5)
    end, 0.5)
end

-------------------------------------------
-- 加速计事件
-------------------------------------------
local function TestAccelerometerEvent(self)
    
end

-------------------------------------------
-- 单点触摸事件
-------------------------------------------
local function TestTouchOneByOne(self)
    local node = display.newSprite("HelloWorld.png")
    self:addChild(node)
    node:setPosition(display.cx, display.cy)

    local function OnTouchOBOBegan(touch, event)
        print("OnTouchOBOBegan", touch, event)
        return true
    end

    local function OnTouchOBOEnded(touch, event)
        local pos = touch:getLocation()
        if cc.rectContainsPoint(node:getBoundingBox(), pos) then
            print("You click the sprite")
        end
    end

    local listener = cc.EventListenerTouchOneByOne:create()
                :setSwallowTouches(true)
    listener:registerScriptHandler(OnTouchOBOBegan, cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(OnTouchOBOEnded, cc.Handler.EVENT_TOUCH_ENDED)
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, node)
end

-------------------------------------------
-- 多点触摸事件
-------------------------------------------
local function TestTouchAllAtOnce(self)
    local node = display.newSprite("HelloWorld.png")
    self:addChild(node)
    node:setPosition(display.cx, display.cy)

    local function OnTouchAAOBegan(touches, event)
        print("OnTouchAAOBegan", #touches, event)
        return true
    end

    local function OnTouchAAOMoved(touches, event)
        print("OnTouchAAOMoved", #touches, event)
    end

    local listener = cc.EventListenerTouchAllAtOnce:create()
    listener:registerScriptHandler(OnTouchAAOBegan, cc.Handler.EVENT_TOUCHES_BEGAN)
    listener:registerScriptHandler(OnTouchAAOMoved, cc.Handler.EVENT_TOUCHES_MOVED)
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, node)
end

function MainScene:onCreate()
    -- 注释其他函数，每次仅仅调用一个函数测试
    -- TestNodeEvent(self)
    -- TestFrameEvent(self)
    -- TestAccelerometerEvent(self)
    TestTouchOneByOne(self)
    -- TestTouchAllAtOnce(self)
end

return MainScene
